<?php

namespace app\admin\controller;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;
use think\Log;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Articlepage extends Backend
{
    
    /**
     * Article模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Article');
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    public function index()
    {
        //设置过滤方法
        //$this->request->filter(['strip_tags', 'htmlspecialchars']);
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = model('ArticleCate')
                ->where($where)->where('type','2')
                ->order($sort, $order)
                ->count();

            $list = model('ArticleCate')
                ->where($where)->where('type','2')
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function edit($ids = NULL)
    {

        $row = $this->model->where('site_id',$ids)->find();
        if ($this->request->isPost())
        {

            Log::record("@@@@@@@@@@@@@@@@@@@@@@@@@@@".$ids);
            $this->code = -1;
            $params = $this->request->post("row/a");

            Log::record(json_encode($params));


            if($row){
                if ($params)
                {
                    foreach ($params as $k => &$v)
                    {
                        $v = is_array($v) ? implode(',', $v) : $v;
                    }
                    try
                    {
                        //是否采用模型验证
                        if ($this->modelValidate)
                        {
                            $name = basename(str_replace('\\', '/', get_class($this->model)));
                            $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                            $row->validate($validate);
                        }
                        $result = $row->save($params);
                        if ($result !== false)
                        {
                            $this->code = 1;
                        }
                        else
                        {
                            $this->msg = $row->getError();
                        }
                    }
                    catch (think\exception\PDOException $e)
                    {
                        $this->msg = $e->getMessage();
                    }
                }
                else
                {
                    $this->msg = __('Parameter %s can not be empty', '');
                }

                return;
            }else{
                if ($params)
                {
                    foreach ($params as $k => &$v)
                    {
                        $v = is_array($v) ? implode(',', $v) : $v;
                    }
                    try
                    {
                        //是否采用模型验证
                        if ($this->modelValidate)
                        {
                            $name = basename(str_replace('\\', '/', get_class($this->model)));
                            $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                            $this->model->validate($validate);
                        }
                        $params['site_id'] = $ids;
                        $params['addtime'] = time();
                        $result = $this->model->save($params);
                        if ($result !== false)
                        {
                            $this->code = 1;
                        }
                        else
                        {
                            $this->msg = $this->model->getError();
                        }
                    }
                    catch (\think\exception\PDOException $e)
                    {
                        $this->msg = $e->getMessage();
                    }
                }
                else
                {
                    $this->msg = __('Parameter %s can not be empty', '');
                }

                return;
            }



        }


        if (!$row){

            $row = array(
                'name'=>'',
                'littitle'=>'',
                'jumpurl'=>'',
                'content'=>'',
                'author'=>'',
                'weigh'=>'99',
                'site_id'=>$ids,
            );
            $this->view->assign("row", $row);
            return $this->view->fetch('edit');
        }



        $this->view->assign("row", $row);
        return $this->view->fetch();
    }



    

}
